package edu.ucla.cs.typecast.util.slf;

import java.io.*;
import java.util.Map;

public class ScriptLogWriter {
	protected BufferedWriter bw;
	
	// True if no entry has been read so far
	
	protected boolean beginning = true;

	public ScriptLogWriter(File f) throws IOException {
		this(f, false);
	}

	public ScriptLogWriter(File f, boolean append) throws IOException {
		beginning = false;
		bw = new BufferedWriter(new FileWriter(f, append));
	}

	public ScriptLogWriter(Writer w) {
		bw = new BufferedWriter(w);
	}

	public void close() throws IOException {
		bw.close();
	}

	public void writeEntry(Map<? extends Object, ? extends Object> entry)
			throws IOException {
		// If not the first row, write entry separator first

		if (!beginning) {
			bw.write("%" + "\n");
		}

		// Write each key-value pair as "<key> = <value>\n"

		for (Map.Entry<? extends Object, ? extends Object> line : entry
				.entrySet()) {

			bw.write(line.getKey() + " = " + line.getValue() + "\n");
		}

		beginning = false;
	}
}
